package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author: cloudy
 * @Description:
 * @Date: Created in 9:44 2021/1/2
 * @ModifiedBy:
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 完善个人基本信息
     *
     * @param param
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody Map<String, String> param,
                                               @RequestHeader("Authorization") String token) {

        try {
            Boolean flag = userInfoService.saveUserInfo(param, token);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("存储基本信息失败", e);
        }

        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("存储用户基本信息失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile file,
                                               @RequestHeader("Authorization") String token) {
        try {
            Boolean flag = userInfoService.saveUserLogo(file, token);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("存储头像失败", e);
        }

        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("存储头像失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
